package com.example.mannortycoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mannortycoon.entity.UserEntity;
import com.example.mannortycoon.service.UserService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@Api(tags="用户")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiImplicitParam(name = "user", value = "用户对象，包含用户名和密码")
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R register(@RequestBody UserEntity userEntity) {
        System.out.println(userEntity);
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username", userEntity.getUsername());
        UserEntity user = userService.getOne(wrapper);
        if (user == null) {
            userEntity.setCreateTime(new Date());
            userService.save(userEntity);
            return R.ok("注册成功");
        } else {
            return R.error("用户名已存在");
        }
    }

    @GetMapping("/selectUsers/{username}")
    @ApiOperation("根据用户名查询用户")
    public R selectUsers(@PathVariable("username") String username){
        List<UserEntity> list = userService.selectUsers(username);
        return R.ok().put("data",list);
    }

    @PostMapping("/login")
    @ApiImplicitParam(name = "user", value = "用户对象，包含用户名和密码")
    @ApiOperation(value = "用户登陆")
    public R login(@RequestBody UserEntity userEntity) {
        return userService.login(userEntity);
    }

    /**
     * app获取用户信息
     */
    @GetMapping("/getUserInfo")
    @ApiOperation(value="app获取用户信息")
    public R getUserInfo(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        UserInfo userInfo = userService.getUserInfo(userId);
        return R.ok().put("data",userInfo);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiIgnore
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{userId}")
    @ApiIgnore
    public R info(@PathVariable("userId") Long userId) {
        UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiIgnore
    public R save(@RequestBody UserEntity user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiIgnore
    public R update(@RequestBody UserEntity user) {
        user.setUpdateTime(new Date());
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiIgnore
    public R delete(@RequestBody Long[] userIds) {
        userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
